﻿using System;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.IO;

namespace Web.CodeTest
{
    public class User
    {
        public static void CodeDomTest()
        {
            // 创建编译器对象
            CSharpCodeProvider p = new CSharpCodeProvider();
        
            
            // 设置编译参数
            CompilerParameters options = new CompilerParameters();
            options.ReferencedAssemblies.Add("System.dll");
            options.GenerateExecutable = false;
            options.OutputAssembly = "CodeDomTest.dll";
            options.GenerateInMemory = true;


            string code = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Web.Entity.Indexer
{
    public class User
    {
        public static void SetFieldsValue()
        {

        }

        public static SqlParameter[] GetSqlParamsValue(Web.Entity.User dataObj)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();

            lstParams.Add(new SqlParameter(""@UserName"",dataObj.UserName));
            lstParams.Add(new SqlParameter(""@RealName"",dataObj.RealName));
            lstParams.Add(new SqlParameter(""@Age"", dataObj.Age));

            return lstParams.ToArray();
        }

        public static string GetTestString()
        {
            return ""3434"";
        }
    }
}

     ";
            CodeSnippetCompileUnit cu = new CodeSnippetCompileUnit(code);

            // 开始编译
            CompilerResults cr = p.CompileAssemblyFromDom(options, cu);

            // 显示编译信息          

        }
    }
}
